package com.ctrip.demo.java8;

import com.alibaba.fastjson.JSON;
import netscape.javascript.JSObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @author xs.wu
 */
public class HashMapTest {

    public static void main(String[] args) {
        // 初始化一组字符串
        List<String> list = new ArrayList<>();
        list.add("jlkk");
        list.add("lopi");
        list.add("小傅哥");
        list.add("e4we");
        list.add("alpo");
        list.add("yhjk");
        list.add("plop");

        // 定义要存放的数组
        String[] tab = new String[8];


        for (String key : list) {
            int idx = key.hashCode() & (tab.length - 1)
                    ;
            System.out.println(String.format("key值=%s Idx = %d", key, idx));
            if (null == tab[idx]) {
                tab[idx] = key;
                continue;
            }
            tab[idx] = tab[idx] + "->" + key;
        }

        System.out.println(JSON.toJSON(tab));

        System.out.println("123".hashCode());

        System.out.println(hash("123"));


        String[] strings = {"走","不走","走","不走","走","不走","走","不走"};

        Random random = new Random();


        for (int i = 0; i < 10; i++) {
            int i1 = random.nextInt(8);
            System.out.println(strings[i1]);
        }
    }


    static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }
}
